package guoan.repository;

import guoan.domain.AutoFormValue;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface AutoFormValueRepository extends PagingAndSortingRepository<AutoFormValue, Long>,
		JpaSpecificationExecutor<AutoFormValue> {

	@Query(value = "SELECT V.`value` FROM auto_form_input I LEFT JOIN auto_form_value V  ON i.id = V.auto_form_input_id WHERE v.form_id = :formId ORDER BY I.order_id DESC", nativeQuery = true)
	List<Object> findByAutoFormInput(@Param("formId") String formId);

	@Query(value = "SELECT 	V.`value`, I.id, I.order_id, I.`name`, I.type FROM auto_form_input I LEFT JOIN auto_form_value V ON i.id = V.auto_form_input_id WHERE v.form_id = :formId ORDER BY 	I.order_id DESC ", nativeQuery = true)
	List<Object[]> findFormValue(@Param("formId") String formId);

	@Modifying
	@Query(value = "DELETE FROM auto_form_value  WHERE auto_form_input_id = :autoFormInputId ", nativeQuery = true)
	void deleteByAutoFormInputId(@Param("autoFormInputId") Long autoFormInputId);

	@Modifying
	@Query(value = "DELETE FROM auto_form_value  WHERE form_id = :form_id ", nativeQuery = true)
	void deleteByFormid(@Param("form_id")  String formid);

	AutoFormValue findOneByAutoFormInputIdAndFormId(Long id, String formId);

}
